package beanstao.util.type.base;

import java.lang.reflect.Field;

import beanstao.util.Util;
import beanstao.util.exception.technical.util.reflect.FailToCastObjectException;
import beanstao.util.type.Castor;

@SuppressWarnings({ "rawtypes" })
public class Number2Enum extends Castor<Number, Enum>
{

	@Override
	public Enum cast(Number src, Class<?> toType, String... args) throws FailToCastObjectException
	{
		try
		{
			for (Field field : toType.getFields())
				if (field.getType() == toType)
				{
					Enum em = (Enum) field.get(null);
					if (em.ordinal() == src.intValue())
						return em;
				}
			throw Util.makeThrow(FailToCastObjectException.class, "Can NO find enum value in [%s] by int value '%d'", toType.getName(), src.intValue());
		}
		catch (Exception e)
		{
			throw new FailToCastObjectException(e.getMessage());
		}
	}

}
